<%@ page import="java.util.*" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.text.DateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="org.tigr.htc.common.*" %>
<%@ page import="org.tigr.htc.cmd.*" %>
<%@ page import="org.tigr.htc.persist.*" %>
<%@ page errorPage="errorpage.jsp?debug=log" %>

<%!
    static String[] colors = new String[] { "#f0f0f0", "#c0c0f0" };
%>
<html>
<head>
<title>HTCRequest Display</title>
</head>

<body>
<%@ include file="header.jsp"%>
<%-- 
<jsp:include page="header.jsp" flush="true"></jsp:include>
--%>
<font size=+2 face="arial,helvetica,sanserif" >Display Request Information</font>
<font size=-1>
<form method="get" action="request_display.jsp">
change to request with id <input type="text" name="RequestID" value=""/>&nbsp;<input type="submit" value="GO" /><br>
</form>
</font>
<%
// IPersist pl = PersistFactory.getIPersist();
IPersist pl = DirectDatabase.getInstance();
long id;
Command cmd;
CommandStatus cstatus;
String title = null;
String sid = request.getParameter("RequestID");
if(sid != null) { 

  title = "Searching for request with id: " + sid;

  // display data for a specific request ID
  id = Long.parseLong(sid);
  cmd = (Command) pl.find(id, Command.class);
  if(cmd == null) {
    title += "\n <br> No matching requests found!";
%>
	<br> <font size=+1 face="arial,helvetica,sanserif" color="red"><%=title %></font>	
  <%}
  else {
    // As the command is not null force the command to load tasks
    Vector tasks = pl.retrieveTasks(id);
    cmd.setTasks(tasks);
	  cstatus = cmd.getStatus();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
%>
<table>
<tr>
<td>
  <table border=1 cellspacing=0 cellpadding=4 >
    <tr bgcolor="#000000"><td colspan=7><strong><font color="white">Request Information</strong></td></tr>
    <tr bgcolor="#c0c0f0"><td>ID</td><td>State</td><td>Username</td><td>Group</td><td>Grid ID</td><td>Submitted from</td><td>Cmd Name(opt)</td></tr>
    <tr><td><%=cmd.getID() %></td><td><%=cmd.getState() %></td>
    <td><%=cmd.getUserName() %></td>
    <td><%=cmd.getGroup() %></td>
    <td><%=cmd.getGridJobID() %></td>
    <td><%=cmd.getSubmitURL() %></td>
    <td><%=cmd.getName() %></td></tr>
  </table><br>
</td>

</tr>
<td>
  <table border=1 cellspacing=0 cellpadding=3 >
     <tr bgcolor="#000000"><td colspan=6><strong><font color="white">Command Status</font></strong></td></tr>
     <tr bgcolor="#c0c0f0"><td>Total Tasks</td><td>Running</td><td>Completed</td><td>Errors</td><td>Message</td></tr>
     <tr>
     <td><%=cmd.getNumTasks() %></td>
     <td><%=cstatus.getRunning() %></td>
     <td><%=cstatus.getCompleted() %></td>
     <td><%=cstatus.getErrors() %></td>
     <td><%=cstatus.getMessage() %></td></tr>
  </table>
</td>
</tr>
<tr>
<td colspan=2>
  <br>
  <table border=1 cellspacing=0 cellspacing=3>
     <tr bgcolor="#000000"><td colspan=2><strong><font color="white">Command Information</font></strong></td></tr>
     <tr><td>Executable</td><td><%=cmd.getCommand() %></td></tr>
<%
    Date drequest = new Date(cmd.getRequestTime());
    String requestTime = "";
    if(drequest != null) requestTime = df.format(drequest);
  
    Date dsubmit = new Date(cmd.getSubmitTime());
    String submit = "";
    if(dsubmit != null) submit = df.format(dsubmit);

    Date dstart = new Date(cmd.getStartTime());
    String start = "";
    if(dstart != null) start = df.format(dstart);

    Date dend = new Date(cmd.getEndTime());
    String end = "";
    if(dend != null) end = df.format(dend);
%>
  
     <tr><td>Request Time</td><td><%=requestTime %></td></tr>
     <tr><td>Submit Time</td><td><%=submit %></td></tr>
     <tr><td>Start Time</td><td><%=start %></td></tr>
     <tr><td>End Time</td><td><%=end %></td></tr>
     <tr><td>Initial Directory (CWD)</td><td><%=cmd.getInitialDir() %></td></tr>
     <tr><td>Standard Output</td><td><%=cmd.getOutput() %></td></tr>
     <tr><td>Standard Error</td><td><%=cmd.getError() %></td></tr>
     <tr><td>Standard Input</td><td><%=cmd.getOutput() %></td></tr>

<% 
    String paramStr = null;
    // If the command has parameters then display params
    if(cmd.getParamsCount() > 0) {
      Iterator params =  cmd.getParams().iterator();
      for( ; params.hasNext(); ) {
	      Param param = (Param) params.next();
	      paramStr = "";
	      if(param.getKey() != null ) {
	        paramStr = param.getKey() + "=";
	      }
	      paramStr += param.getValue();
%>
     <tr><td>Param(<%=param.getType() %>)</td><td><%=paramStr %></td></tr>
<% 
      }
    } // End of if (cmd.getParamsCount() > 0)
%>
     <tr><td>Notify Script</td><td><%=cmd.getNotifyScript() %></td></tr>
   </table>
</td>
</tr>
</table>
<br>
<%
    // Based on the number of tasks for this command display some
    int nt = cmd.getNumTasks();
    
    // If the number of tasks is 0, do not show the table
    if (nt == 0) {
    	// Do nothing
    }
    else if (nt < 6) {
    	// If the number of tasks is less than 6 then display task info  
%>

  	<table border=1 cellspacing=0 cellpadding=3 >
    <tr bgcolor="#000000"><td colspan=7><font color="white"><strong>Task List for <%=id %>  Total <%=nt %></strong></font></td></tr>
    <tr bgcolor="#c0c0f0"><td>Index</td><td>Machine</td><td>State</td><td>Worker</td><td>Return Value</td><td>Message</td></tr>

<%
	    Task task = null;
	    for(int i = 0; i < cmd.getNumTasks(); i++) {
	      	task = cmd.getTask(i);
        	String bb = "http://intranet.tigr.org/sysadmin/bb/html/";
	      	if(task != null) {
		      // If task is not null then display task
		      if(task.machine.endsWith(".tigr.org")) 
		      {
            	bb += task.machine;
          	  } 
          		else 
          		{
			      bb += task.machine + ".tigr.org";
	          	}
%>
    <tr>
    <td><%=task.index %></td>
    <td><%=task.machine %> <a href="<%=bb %>.cpu.html">CPU</a>  <a href="<%=bb %>.memory.html">MEM</a></td>
    <td><%=task.state %></td>
    <td><%=task.worker %></td>
    <td><%=task.returnValue %></td>
    <td><%=task.message %></td>
    </tr>

<%  
        } else {  
%>
    <tr>
    <td>0</td>
    <td>0</td>
    <td>0</td>
    <td>0</td>
    <td>0</td>
    <td>0</td>
    </tr>
<% 
        } 
      } 
%>
    </table>	
<%
    } else {
      	// If the number of tasks is greater than 6 then show the link
      	// to task_display page
%>
  		<a href="task_display.jsp?RequestID=<%=sid %>"> Get Detail List of <%=nt %> Tasks</a><br>
<% 
    } 
%>
<br>	  
<% 
  }  // End of valid request found 
     //   else { 
     // } 
     
// end of if sid is not null
} %>

<jsp:include page="footer.jsp" flush="true"></jsp:include>
<%-- 
<jsp:include page="footer.jsp" flush="true">
</jsp:include>
--%>
</body>
</html>
